package com.info.zhiduoduo.repository.mapper.taxer;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.taxer.TaxerPaymentDto;
import com.info.zhiduoduo.repository.entity.taxer.TaxerPayment;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author bob
 * @since 2019-11-16
 */
public interface TaxerPaymentMapper extends BaseMapper<TaxerPayment> {

	@Select(" select tp.*,ti.applyno,ti.amount as invoiceAmount,ti.service_charge,ti.tax_fee,ti.receipt_img," +
			" ma.realname,ms.username,sm.managername,m.maker_type,ifnull(ti.whether_supplement,0) as whether_supplement " +
			" from zdd_taxer_payment tp " +
			" join zdd_maker_account ma on ma.userid = tp.userid " +
			" left join zdd_taxer_invoice ti on ti.invoiceid = tp.invoiceid and ti.deleted = 0 " +
			" left join zdd_maker_studio ms on ms.userid = tp.userid " +
			" join zdd_maker m on m.userid = tp.userid " +
			" left join zdd_system_manager sm on sm.managerid = tp.managerid " +
			" left join zdd_taxer_invoice_project ip on ip.projectid = ti.projectid" +
			" where if(#{type}=0, 1=1, tp.type = #{type}) " +
			" and if(#{state}=0, 1=1, tp.state = #{state}) " +
			" and tp.createtime between #{startDate} and #{endDate} " +
			" and (ma.realname like '%${name}%' or ms.username like '%${name}%') " +
			" and tp.deleted = 0 " +
			" order by tp.payid desc ")
	List<TaxerPaymentDto> getPaymentList(int type, int state, String name, Date startDate, Date endDate);

	@Select(" select * from zdd_taxer_payment where payno = #{payno}")
	TaxerPayment findOneByPayno(String payno);

	@Select(" select * from zdd_taxer_payment where userid = #{userid} and type = #{type} and deleted = 0 order by payid desc limit 1 ")
	TaxerPayment findRegisterPayment(int userid, int type);

	@Select(" select * from zdd_taxer_payment where userid = #{userid} and invoiceid = #{invoiceid} and deleted = 0 order by payid desc limit 1 ")
	TaxerPayment findInvoicePayment(int userid, int invoiceid);

	@Delete(" delete from zdd_taxer_payment " +
			" where userid = #{userid} " +
			" and YEAR(createtime) = YEAR(NOW()) " +
			" and QUARTER(createtime) = QUARTER(#{quarter})")
	int deleteQuarterData(@Param("userid") int userid, @Param("quarter") int quarter);

	@Delete(" delete from zdd_taxer_payment " +
			" where userid = #{userid} " +
			" and YEAR(createtime) = YEAR(NOW()) ")
	int deleteYearData(@Param("userid") int userid);
}
